package com.zuoshenge.sys.handler;

import com.zuoshenge.sys.expection.BadRequestException;
import com.zuoshenge.sys.expection.TokenExpiredException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(BadRequestException.class)
    public ResponseEntity<Object> handleBadRequestException(BadRequestException ex) {
        Map<String, Object> errorDetails = new HashMap<>();
        errorDetails.put("message", ex.getMessage());  
        errorDetails.put("status", ex.getStatus()); // 注意：你可能需要添加一个方法来获取这个值  
        return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(TokenExpiredException.class)
    public ResponseEntity<Object> handleTokenExpiredException(TokenExpiredException te) {
        Map<String, Object> errorDetails = new HashMap<>();
        errorDetails.put("message", te.getMessage());
        errorDetails.put("status", te.getStatus()); // 注意：你可能需要添加一个方法来获取这个值
        return new ResponseEntity<>(errorDetails, HttpStatus.UNAUTHORIZED);
    }

//    @ExceptionHandler(RuntimeException.class)
//    public ResponseEntity<Object> handleRunTimeException(RuntimeException re) {
//        Map<String, Object> errorDetails = new HashMap<>();
//        errorDetails.put("message", re.getMessage());
//        return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
//    }
}